package com.stack;

import java.util.Arrays;

public class MyStack implements MStack {
    public int []elem;
    public int size;

     MyStack() {
        elem=new int[10];
    }

    public void push(int val){
        //空间满
        if(full()){
            //扩容
            exsize();
        }
        //第一个元素
        elem[size++]=val;
    }

    @Override
    public boolean empty() {
        return size==0;
    }

    @Override
    public boolean full() {
        return size==elem.length;
    }

    @Override
    public int top() {
        return elem[size-1];
    }

    @Override
    public void pop() {
        if(empty()){
            throw new RuntimeException("stack empty");
        }
        size--;
    }

    @Override
    public void exsize() {
        elem= Arrays.copyOf(elem,2*elem.length);
    }

    @Override
    public void info() {
        while(!empty()){
            System.out.println(top());
            pop();
        }
    }
}
